﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter21.Code02.Example06
{
    using System.IO;
    using System.IO.Compression;

    public class Compressor
    {
        public static void SaveCompressedFile(string fileName, string data)
        {
            FileStream aFile = new FileStream(fileName, FileMode.Create, FileAccess.Write);
            GZipStream compressionStream = new GZipStream(aFile, CompressionMode.Compress);
            StreamWriter write = new StreamWriter(compressionStream);
            write.Write(data);
            write.Close();
            compressionStream.Close();
            aFile.Close();
        }

        public static string LoadCompressedFile(string fileName)
        {
            FileStream aFlie = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            GZipStream compressionStream = new GZipStream(aFlie, CompressionMode.Decompress);
            StreamReader reader = new StreamReader(compressionStream);
            string data = reader.ReadToEnd();
            reader.Close();
            compressionStream.Close();
            aFlie.Close();
            return data;
        }
    }
}
